#include "mc31xx_hal_gpio.h"


/**
  * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  * @param  GPIOx: where x can be (A..D depending on device used) to select the GPIO peripheral
  * @param  GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  *         the configuration information for the specified GPIO peripheral.
  * @retval None
  */
void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
{
    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
    //外设使能、特权、分频
    GPIOx->CR = INTDEV_RUN | INTDEV_IS_GROUP0 | INTDEV_CLK_IS_CORECLK_DIV4;
    //输入、输出、上拉
    if (GPIO_Init->InputEn)   GPIOx->IEN = GPIO_Init->Pin | GPIO_SET_ENABLE;
    else                      GPIOx->IEN = GPIO_Init->Pin | GPIO_SET_DISABLE;
    if (GPIO_Init->OutputEn)  GPIOx->OEN = GPIO_Init->Pin | GPIO_SET_ENABLE;
    else                      GPIOx->OEN = GPIO_Init->Pin | GPIO_SET_DISABLE;
    if (GPIO_Init->PullupEn)  GPIOx->PUEN = GPIO_Init->Pin | GPIO_SET_ENABLE;
    else                      GPIOx->PUEN = GPIO_Init->Pin | GPIO_SET_DISABLE;

}

/**
  * @brief  De-initializes the GPIOx peripheral registers to their default reset values.
  * @param  GPIOx: where x can be (A..D depending on device used) to select the GPIO peripheral
  * @param  GPIO_Pin: specifies the port bit to be written.
  *         This parameter can be one of GPIO_PIN_x where x can be (0..15).
  * @retval None
  */
void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
{
    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Pin));

    GPIOx->BRR = GPIO_Pin;
    GPIOx->IEN = GPIO_Pin | GPIO_SET_DISABLE;
    GPIOx->OEN = GPIO_Pin | GPIO_SET_DISABLE;
    GPIOx->PUEN = GPIO_Pin | GPIO_SET_DISABLE;
}

/**
  * @brief  Reads the specified input port pin.
  * @param  GPIOx: where x can be (A..D depending on device used) to select the GPIO peripheral
  * @param  GPIO_Pin: specifies the port bit to read.
  *         This parameter can be GPIO_PIN_x where x can be (0..15).
  * @retval The input port pin value.
  */
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
{
    GPIO_PinState bitstatus;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Pin));

    if ((GPIOx->SIDR & (GPIO_Pin>>16)) != (uint32_t)GPIO_PIN_RESET)
    {
        bitstatus = GPIO_PIN_SET;
    }
    else
    {
        bitstatus = GPIO_PIN_RESET;
    }
    return bitstatus;
}

/**
  * @brief  Sets or clears the selected data port bit.
 
  * @param  GPIOx: where x can be (A..D depending on device used) to select the GPIO peripheral
  * @param  GPIO_Pin: specifies the port bit to be written.
  *          This parameter can be one of GPIO_PIN_x where x can be (0..15).
  * @param  PinState: specifies the value to be written to the selected bit.
  *          This parameter can be one of the GPIO_PinState enum values:
  *            @arg GPIO_PIN_RESET: to clear the port pin
  *            @arg GPIO_PIN_SET: to set the port pin
  * @retval None
  */
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin, GPIO_PinState PinState)
{
    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Pin));
    assert_param(IS_GPIO_PIN_ACTION(PinState));

    if (PinState != GPIO_PIN_RESET)
    {
        GPIOx->BSR = GPIO_Pin;
    }
    else
    {
        GPIOx->BRR = GPIO_Pin;
    }
}

/**
  * @brief  Toggles the specified GPIO pin
  * @param  GPIOx: where x can be (A..D depending on device used) to select the GPIO peripheral
  * @param  GPIO_Pin: Specifies the pins to be toggled.
  * @retval None
  */
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
{
    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Pin));

    GPIOx->BIR = (uint32_t)GPIO_Pin;
}

//设置IO方向
void HAL_GPIO_SetInOut(GPIO_TypeDef* GPIOx, uint32_t GPIO_Pin, FunctionalState Input, FunctionalState Output)
{
    /* Check the parameters */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
    assert_param(IS_GPIO_PIN(GPIO_Pin));

    if (Input == ENABLE)    GPIOx->IEN = GPIO_Pin | GPIO_SET_ENABLE;
    else                    GPIOx->IEN = GPIO_Pin | GPIO_SET_DISABLE;
    if (Output == ENABLE)   GPIOx->OEN = GPIO_Pin | GPIO_SET_ENABLE;
    else                    GPIOx->OEN = GPIO_Pin | GPIO_SET_DISABLE;
}


